package cn.edu.zafu.bigdata.x.huangxihao202211080211.UDP1;

import java.net.*;

public class ClockClient11 {
    public static void main(String[] args) {
        // 设置服务端地址和端口号
        String serverAddress = "127.0.0.1"; // 可以修改为服务端的实际IP地址
        int port = 12345;

        try (DatagramSocket socket = new DatagramSocket()) {
            InetAddress serverInetAddress = InetAddress.getByName(serverAddress);

            // 发送请求给服务端
            String requestMessage = "GET TIME";
            byte[] sendData = requestMessage.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverInetAddress, port);
            socket.send(sendPacket);
            System.out.println("已发送请求: " + requestMessage);

            // 接收来自服务端的时间响应
            byte[] receiveData = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            socket.receive(receivePacket);
            String serverTime = new String(receivePacket.getData(), 0, receivePacket.getLength());

            System.out.println("从服务端收到时间: " + serverTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

